package com.soft.javase.day06.string;

import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/24
 */
public class StringDemo3 {
    public static void main(String[] args) {
        System.out.println("-----用StringBuilder把数组拼接成一个字符串-----");
        int[] arr = {1, 2, 3};
        StringBuilder sb1 = new StringBuilder();
        sb1.append("[");
        for (int i = 0; i < arr.length; i++) {
            sb1.append(arr[i]);

            // 如果不是最后一个元素，添加逗号和空格
            if (i < arr.length - 1) {
                sb1.append(", ");
            }
        }
        sb1.append("]");
        System.out.println(sb1);

        System.out.println("\n-----利用StringBuilder完成字符串反转-----");
        String origin = "Hello, World!";
        StringBuilder sb2 = new StringBuilder(origin);
        String reverse = sb2.reverse().toString();
        System.out.println("原始字符串: " + origin);
        System.out.println("反转字符串: " + reverse);

        String original = "Hello, World!";
        StringBuilder sb = new StringBuilder();
        for (int i = original.length() - 1; i >= 0; i--) {
            sb.append(original.charAt(i));
        }
        String reversed = sb.toString();
        System.out.println("原始字符串: " + original);
        System.out.println("反转字符串: " + reversed);

        System.out.println("\n-----判断一个字符串是否是对称字符串-----");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str = sc.next();
        if (str.equals(new StringBuilder(str).reverse().toString())){
            System.out.println("该字符串是对称字符串");
        }else {
            System.out.println("该字符串不是对称字符串");
        }

    }
}
